<template>
  <DataTable searchPlaceholder="请输入相关信息" :columns="columns" dialogTitle="医生排班信息表" :formFields="formFields"
    :tableData="userlData" apiUrl="arrange" :putUser="putUser" :addUser="addUser" :delUser="delUser" />
</template>

<script setup>
import DataTable from '../../components/DataTable.vue';
import { ref, onMounted } from "vue";
import axios from "axios";

// 定义表格列配置
const columns = [
  { prop: 'nickName', label: '医生姓名', width: '100px' },
  { prop: 'officeName', label: '科室', width: '100px' },
  { prop: 'visitorsNumber', label: '就诊数量', width: '100px' },
  { prop: 'cost', label: '挂号费', width: '100px' },
  { prop: 'arrangeDate', label: '日期', width: 'show-overflow-tooltip' },
]

// 定义表单字段配置
const formFields = [
  { label: '医生姓名', model: 'doctorId', placeholder: '请输入生姓名' },
  { label: '科室', model: 'officeId', placeholder: '请输入科室' },
  { label: '就诊数量', model: 'visitorsNumber', placeholder: '请输入就诊数量' },
  { label: '挂号费', model: 'cost', placeholder: '请输入挂号费' },
  { label: '日期', model: 'arrangeDate', placeholder: '请输入日期' }
];
// 定义用户数据
const userlData = ref([])

// 获取数据
async function getUserData() {
  try {
    const response = await axios.get('api/arrange/getAll')
    
    userlData.value = response.data.data
    .filter(item => item.isDelActive === 0)
    // .map(item => ({
    //   ...item,
    //   arrangeStatus: item.isDelActive === 1 ? '已取消排班' : '正常排班'
    // }));
    console.log("排班数据:", userlData)
  } catch (error) {
    console.log(error)
  }
}

// 新增数据
async function addUser(formValue) {
  console.log("new")
  console.log(formValue)

  try {
    await axios.post("api/arrange/addArrange", formValue);
    console.log("医生排班新增", formValue)
    location.reload()
  } catch (error) {
    console.error("新增医生排班时出错:", error);
  }
}

// 编辑数据
async function putUser(formValue) {
  console.log("排班编辑formValue", formValue)
  try {
    await axios.put(`api/arrange/putArrange/${formValue.id}`, formValue);
    console.log("医生排班编辑", formValue)
    location.reload()
  } catch (error) {
    console.error("编辑医生排班时出错:", error);
  }
}

// 删除数据确
async function delUser(index, row) {
  try {
    await axios.delete(`api/arrange/delArrange/${row.id}`);
    console.log("医生排班删除：", row.id)
  } catch (error) {
    console.error("删除医生排班时出错:", error);
  }
}
onMounted(() => {
  getUserData()
})
</script>
